/****** Object:  StoredProcedure [dbo].[web_upd_voucher]    Script Date: 10/19/2013 09:22:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'web_upd_voucher')
DROP PROCEDURE web_upd_voucher
GO

-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[web_upd_voucher] 
@galvosid varchar(100),
@apmokejimorusis varchar(100),
@kortelesnr varchar(100),
@data varchar(50)

AS
BEGIN
DECLARE @galvaid int


SET NOCOUNT ON

BEGIN TRANSACTION



SET @galvaid  = cast(@galvosid as int)

update Kvitogalva 
set apmokejimorusis=@apmokejimorusis,
kortelesnr=@kortelesnr,
data=@data,
import=2,
year=year(@data),
month=month(@data),
day=day(@data),
hour=DATEPART(hh, @data),
minute=DATEPART(mi,@data)
where ID = @galvosid


DECLARE @barkodas varchar(100)
DECLARE @kiekis varchar(100)
DECLARE @kaina varchar(100)
DECLARE @mokesciai varchar(100)
DECLARE @suma decimal(12,2)
DECLARE @mok decimal(12,2)
DECLARE @eilid int

DECLARE MY_CURSOR CURSOR 
  LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR 
SELECT barkodas,kiekis,kaina,mokesciai,eilid
FROM #tt_eilutes

OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @barkodas,@kiekis,@kaina,@mokesciai,@eilid
WHILE @@FETCH_STATUS = 0
BEGIN 

SET @suma = cast(isnull(@kaina,0) as decimal(12,2))/cast(isnull(@kiekis,1) as decimal(12,2))
SET @mok = cast(isnull(@kaina,0) as decimal(12,2))-(cast(isnull(@kaina,0) as decimal(12,2))*100/(100+cast(@mokesciai as numeric(10,2)))) 

update Kvitoeilute 
set barkodas=@barkodas,kiekis=@kiekis,kaina=@suma,mokesciai=@mok,suma=cast(isnull(@kaina,0) as decimal(12,2)) 
where GalvosID = @galvosid AND ID = @eilid

FETCH NEXT FROM MY_CURSOR INTO @barkodas,@kiekis,@kaina,@mokesciai,@eilid
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR

update Kvitogalva set 
suma=(select sum(suma) from Kvitoeilute where galvosid=@galvaid),
mokesciai=(select sum(mokesciai) from Kvitoeilute where galvosid=@galvaid) where id = @galvaid

COMMIT TRANSACTION

END

GO



